<?php


namespace App\Http\Controllers;


use App\Models\LuckTendency;
use Illuminate\Http\Request;

/**
 * 求签，运势
 * Class LuckTendencyController
 * @package App\Http\Controllers
 */
class LuckTendencyController extends Controller
{
    /**
     * 今日求签记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function record(Request $request)
    {
        $data = LuckTendency::where(['uid' => $request->uid, 'date' => date('Y-m-d')])->first();
        return $this->success([
            'used' => $data ? true : false,
            'content' => $data ? $data->tendency : ''
        ]);
    }

    /**
     * 求签
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function tendency(Request $request)
    {
        $txt = [
            '巨物脱钩|手机打窝',
            '瑞气腾腾最吉祥|行人有利即还乡',
            '凡事安守|谨慎小心',
            '在家富贵足安然|出外如同透碧天',
            '两人同行|出门有益',
            '渭川头白一渔翁|遇合来时便不同'
        ];
        shuffle($txt);
        $tendency = current($txt);
        if (!$request->nickname) {
            $data = LuckTendency::where(['uid' => $request->uid, 'date' => date('Y-m-d')])->first();
            if ($data) {
                $tendency = $data->tendency;
            } else {
                $luckTendency = new LuckTendency();
                $luckTendency->uid = $request->uid;
                $luckTendency->date = date('Y-m-d');
                $luckTendency->tendency = $tendency;
                $luckTendency->save();
            }
        }
        return $this->success([
            'content' => $tendency
        ]);
    }
}